/*
 * SaverRunner.java
 *
 * Created on 11 de septiembre de 2007, 18:36
 */

package net.javahispano.jsignalwb.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.*;

import net.javahispano.jsignalwb.JSWBManager;
import net.javahispano.jsignalwb.plugins.Saver;
import net.javahispano.jsignalwb.plugins.framework.SaverRunner;
import javax.swing.SwingWorker.StateValue;

/**
 *
 * @author  Compaq_Propietario
 */
public class SaverExecutionJDialog extends javax.swing.JDialog implements PropertyChangeListener {
    private Saver saver;
    private SwingWorker swingWorker;
    /** Creates new form SaverRunner */
    public SaverExecutionJDialog(Saver saver, File file) {
        super(JSWBManager.getJSWBManagerInstance().getParentWindow(), "Saver");
        JSWBManager jswbManager = JSWBManager.getJSWBManagerInstance();
        setModal(true);
        this.saver = saver;
        initComponents();

        SaverRunner sr = new SaverRunner(saver, file);
        sr.addPropertyChangeListener(this);
        setLocationRelativeTo(this.getOwner());
        swingWorker = (SwingWorker) sr;
        jswbManager.setJSMIgnoreRepaintMode(true);
        sr.execute();
        setVisible(true);
        jswbManager.setJSMIgnoreRepaintMode(false);
    }

    /**
     * Detecta cuando ha finalizado la ejecucion del {@link Saver }
     * @param evt {@link PropertyChangeEvent} informa del estado de ejecucion
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if ("state".equals(evt.getPropertyName())) {
            if (evt.getNewValue().equals(StateValue.DONE)) {
                this.dispose();
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Codigo Generado  ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Saving....");

        jProgressBar1.setIndeterminate(true);

        jButton1.setText("Stop");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                          .addContainerGap()
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 114,
                                                  Short.MAX_VALUE))
                          .addContainerGap())
                .addGroup(layout.createSequentialGroup()
                          .addGap(33, 33, 33)
                          .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                          .addGap(36, 36, 36))
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                          .addContainerGap()
                          .addComponent(jLabel1)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(jButton1)
                          .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );
        pack();
    } // </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_jButton1ActionPerformed
        saver.cancelExecution();
        swingWorker.cancel(false);
    } //GEN-LAST:event_jButton1ActionPerformed


    // Declaracion de varibales -no modificar//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jProgressBar1;
    // Fin de declaracion de variables//GEN-END:variables

}
